package me.zhengjie

import me.zhengjie.common.utils.StringUtils
import me.zhengjie.common.utils.cypto.RSAUtils
import me.zhengjie.modules.security.utils.JwtTokenUtil
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.test.context.SpringBootTest
import java.nio.charset.StandardCharsets

@EnableConfigurationProperties
@SpringBootTest(classes = [JwtTokenUtil::class])
class ApplicationTests {

	@Autowired
	val jwtUtil: JwtTokenUtil?=null

	@Test
	fun contextLoads() {
		Assertions.assertEquals(3, 3) {
			"Three does not equal four"
		}
	}

	@Test
	fun RSAUtilsTest() {
		var keySize = RSAUtils.RAS_KEY_SIZE
		if (keySize < RSAUtils.RAS_KEY_SIZE) keySize = RSAUtils.RAS_KEY_SIZE
		val pair = RSAUtils.genKeyPair(keySize)
		//生成公钥和私钥
		val publicKey = pair!!.key
		val privateKey = pair.value
		println("key generate success!")
		println("privateKey=" + RSAUtils.encodeBase64(privateKey))
		println("publicKey=" + RSAUtils.encodeBase64(publicKey))

		//明文
		var ming = "这是一段测试文字。。。。".toByteArray(StandardCharsets.UTF_8)
		println("明文:" + String(ming, StandardCharsets.UTF_8))

		//加密后的密文
		val mi = RSAUtils.encryptByPublicKey(ming, publicKey)
		println("密文:" + String(mi, StandardCharsets.UTF_8))
		//解密后的明文
		ming = RSAUtils.decryptByPrivateKey(mi, privateKey)
		println("解密:" + String(ming, StandardCharsets.UTF_8))
	}

	@Test
	fun getCityTest(){
		var c = StringUtils.getCityInfo("172.18.112.1")
		println("城市：${c}")
	}

	@Test
	fun jwtUtilTest(){
		var claims:Map<String, Any> = HashMap()
		val token: String = jwtUtil!!.doGenerateToken(claims,"admin")
		println("token = $token")

		claims = jwtUtil!!.getAllClaimsFromToken(token)
		println("claims = $claims")

		val username: String = claims.get("sub").toString()
		println("username = $username")
	}
}
